// 注意输入输出
#include <fstream>
#include <iostream>
#include <vector>
#include <algorithm>
#include<stdio.h>
using namespace std;

int max_value = 1<<16;

int get_result(vector<int> data)
{
    if (data.size()<2)
        return 0;
    if (data.size()==2){
        return abs(data[0] - data[1]);
    }
    int n = data.size() / 2;
    int result = max_value;
    do{
        if (data[0]==0||data[n]==0)
            continue;
        int a = 0;
        for (int i = 0; i < n;++i){
            a = a*10+data[i];
        }
        int b = 0;
        for (int i = n; i < data.size();++i){
            b = b * 10 + data[i];
        }
        result = min(result, abs(a - b));
    }
    while(next_permutation(data.begin(), data.end()));
    return result;
}
int main()
{
#ifndef ONLINE_JUDGE 
    fstream cin("test.txt");
#endif
    int n;
    cin >> n;
    while(1){
        if (cin.get()=='\n')
            break;
    };
    vector<int> temp_data;
    for (int i = 0; i < n;++i){
        temp_data.clear();
        char temp;
        while(temp=cin.get()){
            if (temp>='0'&&temp<='9')
                temp_data.push_back(temp-'0');
            if (temp=='\n')
                break;
        }
        cout << get_result(temp_data) << endl;
    }
    return 0;
}